(function (Cofe, Spine, $) {
    var Models = Cofe.Models;
    var File = Models.File;
    var Folder = Models.Folder;

    var EntryList = Spine.Model.setup("EntryList",  //Models name
    ["status", "entries", "url"]);  //Models attributes.

    EntryList.extend(Spine.Model.Local);

    EntryList.include({
        loadEntries: function (entryData) {
            var folders = new Array();
            var files = new Array();
            var folderIdx = 0;
            var fileIdx = 0;

            for (var i = 0, il = entryData.length; i < il; i++) {
                var currentEntry = entryData[i];
                var newEntry;
                if (currentEntry.basetype == "Folder") {
                    newEntry = Folder.init(currentEntry);
                    folders[folderIdx++] = newEntry;
                }
                else {
                    newEntry = File.init(currentEntry);
                    files[fileIdx++] = newEntry;
                }
            }

            this.folders = folders;
            this.files = files;
        },

        load: function (data) {
            this["status"] = data.status;
            //this["url"] = data.url;
            this["entries"] = data.entries;
            this.loadEntries(data.entries);
            //            this.updateAttributes({
            //                status: data.status,
            //                entries: listEntries
            //            });
        },
        init: function (data) {
            this.url = this.id = data.url;
            this.status = data.status;
            this.entries = data.entries;
            if (data.entries)
                this.loadEntries(data.entries);
            console.log("EntryList created:" + data.url);
        }

    });

    var ajaxSync2 = function (method, Models) {
        if (Models._noSync) return;

        var params = {
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            processData: false
        };

        params.url = Models.url;

        if (!params.url) throw ("Invalid URL");

        if (method == "read")
            params.success = function (data) {
                (Models.refresh || Models.load).call(Models, data);
                Models.trigger("fetched", Models);
            };

        params.error = function (xhr, s, e) {
            Models.trigger("ajaxError", xhr, s, e);
        };

        $.ajax(params);
    };

    EntryList.include({
        url: "",
        fetch: function () {
            ajaxSync2("read", this);
        }
    });


    if (typeof exports !== "undefined") {
        Models.EntryList = exports;
    } else {
        Models.EntryList = EntryList;
    }

})(Cofe, Spine, Spine.$);